package controllers

import (
	"github.com/astaxie/beego"
         m"xmy/models/admin"
	"fmt"
	"github.com/tealeg/xlsx"
)

type EntryController struct {
	beego.Controller
}

func (c *EntryController)  EntryManage() {
	fmt.Println("进来--报名管理--控制器啦！")
	c.TplName = "marketing/entryManage.html"
}

//报名管理列表
func (this *EntryController)  EntryManagePage() {
	fmt.Println("进来--活动列表--控制器啦！")
	page, _ := this.GetInt64("page")
	fmt.Println("page:", page)
	page_size, _ := this.GetInt64("rows")
	fmt.Println("page_size:", page_size)
	sort := this.GetString("sort")
	fmt.Println("sort:", sort)
	order := this.GetString("order")
	fmt.Println("order:", order)
	if len(order) > 0 {
		if order == "desc" {
			sort = "-" + sort
		}
	} else {
		sort = "Id"
	}
	nodes, count := m.GetEntrylist(page, page_size, sort)
	this.Data["json"] = &map[string]interface{}{"total": count, "rows": &nodes}
	fmt.Println("........................",this.Data["json"])
	this.ServeJSON()
	return

}
//报名申请单导出Excel
func (this *EntryController)  EntryExcel() {
	fmt.Println("进来--活动列表--控制器啦！")
	page, _ := this.GetInt64("page")
	fmt.Println("page:", page)
	page_size, _ := this.GetInt64("rows")
	fmt.Println("page_size:", page_size)
	sort := this.GetString("sort")
	fmt.Println("sort:", sort)
	order := this.GetString("order")
	fmt.Println("order:", order)
	if len(order) > 0 {
		if order == "desc" {
			sort = "-" + sort
		}
	} else {
		sort = "Id"
	}
	nodes, count := m.GetEntrylist(page, page_size, sort)

	var file *xlsx.File
	var sheet *xlsx.Sheet
	var row *xlsx.Row
	var cell *xlsx.Cell
	var err error
	file = xlsx.NewFile()
	sheet,err = file.AddSheet("Sheet1")
	row = sheet.AddRow()
	cell= row.AddCell()
	cell.Value = "报名表ID"
	cell= row.AddCell()
	cell.Value = "报名人姓名"
	cell= row.AddCell()
	cell.Value = "手机号"
	cell= row.AddCell()
	cell.Value = "人员数量"
	cell= row.AddCell()
	cell.Value = "报名活动"
	cell= row.AddCell()
	cell.Value = "申请时间"
	for _,v:=range nodes{
		row = sheet.AddRow()
		cell= row.AddCell()
		cell.Value = v["EntryNo"].(string)
		cell= row.AddCell()
		cell.Value = v["EntryName"].(string)
		cell= row.AddCell()
		cell.Value = v["EntryTel"].(string)
		cell= row.AddCell()
		cell.Value = v["EntryCount"].(string)
		cell= row.AddCell()
		cell.Value = v["EntryActivity"].(string)
		cell= row.AddCell()
		cell.Value = v["EntryTime"].(string)

	}

	err = file.Save("static/Excel/报名申请单.xlsx")
	if err != nil {
		fmt.Printf(err.Error())
	}
	this.Data["json"] = &map[string]interface{}{"total": count, "rows": &nodes}
	fmt.Println("........................",this.Data["json"])
	this.ServeJSON()
	return

}